package net.pws.oos.biz.query.impl;

import java.util.ArrayList;
import java.util.List;

import net.pws.common.util.DateUtils;
import net.pws.common.util.StringUtils;
import net.pws.common.persistence.IPage;
import net.pws.common.persistence.QueryRepository;
import net.pws.oos.biz.model.Message;
import net.pws.oos.biz.query.MessageFinder;
import net.pws.oos.biz.query.parameter.MessageQueryParameter;

import org.springframework.stereotype.Service;

@Service
public class DefaultMessageFinder extends AbstractFinder implements
                                                        MessageFinder {
    
    @Override
    public int countMessage(MessageQueryParameter queryParameter) {
        StringBuilder hql = new StringBuilder(QueryRepository.getInstance()
                                                             .get("hql.countMessage"));
        List<Object> params = new ArrayList<Object>();
        buildQueryRequest(hql, params, queryParameter);
        Number count = (Number) entityManager.findUnique(hql.toString(),
                                                         params.toArray());
        return count.intValue();
    }
    
    @SuppressWarnings("unchecked")
    @Override
    public List<Message> listMessage(MessageQueryParameter queryParameter) {
        StringBuilder hql = new StringBuilder(QueryRepository.getInstance()
                                                             .get("hql.listMessage"));
        List<Object> params = new ArrayList<Object>();
        buildQueryRequest(hql, params, queryParameter);
        hql.append(" order by createTime desc");
        IPage<Message> page = entityManager.findPage(hql.toString(),
                                                     params.toArray(),
                                                     queryParameter.getStart(),
                                                     queryParameter.getLimit());
        
        return page.getData();
    }
    
    @Override
    public Message findOne(MessageQueryParameter queryParameter) {
        StringBuilder hql = new StringBuilder(QueryRepository.getInstance()
                                                             .get("hql.listMessage"));
        List<Object> params = new ArrayList<Object>();
        buildQueryRequest(hql, params, queryParameter);
        return (Message) entityManager.findFirst(hql.toString(),
                                                 params.toArray());
    }
    
    private void buildQueryRequest(StringBuilder hql,
                                   List<Object> params,
                                   MessageQueryParameter queryParameter) {
        if (StringUtils.isNotBlank(queryParameter.getName())) {
            hql.append(" and name like ? ");
            String like = "%" + queryParameter.getName() + "%";
            params.add(like);
        }
        if (StringUtils.isNotBlank(queryParameter.getUserId())) {
            hql.append(" and userId=? ");
            params.add(queryParameter.getUserId());
        }
        if (StringUtils.isNotBlank(queryParameter.getUrl())) {
            hql.append(" and url=? ");
            params.add(queryParameter.getUrl());
        }
        if (queryParameter.getRead() != null) {
            hql.append(" and read=? ");
            params.add(queryParameter.getRead());
        }
        
        if (queryParameter.getCreateStartTime() != null) {
            hql.append(" and createTime>=? ");
            params.add(queryParameter.getCreateStartTime());
        }
        if (queryParameter.getCreateEndTime() != null) {
            hql.append(" and createTime<? ");
            params.add(DateUtils.addDay(queryParameter.getCreateEndTime(), 1));
        }
        
    }
    
}
